//////////////////////////////////////////////////////////////////////////
// $Id: MVar.cpp,v 1.1 2009/04/03 12:46:10 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#include "Local.h"

//////////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////////

NAMESPACE_BEGIN

//////////////////////////////////////////////////////////////////////////

	INLINE MVar::MVar()
	{
		GUARD(MVar::MVar());

		memset(&val,0,sizeof(val));

		UNGUARD;
	}
	
	INLINE MVar::MVar(const MVar& var) 
	{
		GUARD(MVar::MVar(const MVar& var));

		memset(&val,0,sizeof(val));
		val.vt = var.val.vt;
		switch (var.val.vt) 
		{
		case VT_VOID:
			break;
		case VT_NULL:
			break;
		case VT_I8:
			val.vi8 = var.val.vi8;
			break;
		case VT_U8:
			val.vu8 = var.val.vu8;
			break;
		case VT_I16:
			val.vi16 = var.val.vi16;
			break;
		case VT_U16:
			val.vu16 = var.val.vu16;
			break;
		case VT_I32:
			val.vi32 = var.val.vi32;
			break;
		case VT_U32:
			val.vu32 = var.val.vu32;
			break;
		case VT_I64:
			val.vi64 = var.val.vi64;
			break;
		case VT_U64:
			val.vu64 = var.val.vu64;
			break;
		case VT_F32:
			val.vf32 = var.val.vf32;
			break;
		case VT_F64:
			val.vf64 = var.val.vf64;
			break;
		case VT_STR:
			val.vstr = NULL;
			if( var.val.vstr )
			{
				U32 len = strlen(var.val.vstr);
				if(len)
				{
					val.vstr = MNEW(CHAR[len+1]); CHECK(val.vstr);
					memset(val.vstr,0,(len+1)*sizeof(CHAR));
					strncpy(val.vstr,var.val.vstr,len);
				}
			}
			break;
		case VT_WSTR:
			val.vwstr = NULL;
			if( var.val.vwstr )
			{
				U32 len = wcslen(var.val.vwstr);
				if( len )
				{
					val.vwstr = MNEW(WCHAR[len+1]); CHECK(val.vwstr);
					memset(val.vwstr,0,(len+1)*sizeof(WCHAR));
					wcsncpy(val.vwstr,var.val.vwstr,len);
				}
			}
			break;
		case VT_OBJ:
			val.vobj = NULL;
			if( var.val.vobj )
			{
				val.vobj = var.val.vobj;
				val.vobj->ref();
			}
			break;
		default:
			MAssert(VA("Error : the type(%d) is unknown!",var.val.vt));
			break;
		}

		UNGUARD;
	}

	INLINE MVar::MVar(CHAR v)
	{
		GUARD(MVar::MVar(CHAR v));

		memset(&val,0,sizeof(val));
		val.vt = VT_I8;
		val.vi8 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(U8 v)
	{
		GUARD(MVar::MVar(U8 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_U8;
		val.vu8 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(I16 v)
	{
		GUARD(MVar::MVar(I16 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_I16;
		val.vi16 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(U16 v) 
	{
		GUARD(MVar::MVar(U16 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_U16;
		val.vu16 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(I32 v)
	{
		GUARD(MVar::MVar(I32 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_I32;
		val.vi32  = v;

		UNGUARD;
	}

	INLINE MVar::MVar(U32 v)
	{
		GUARD(MVar::MVar(U32 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_U32;
		val.vu32 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(I64 v)
	{
		GUARD(MVar::MVar(I64 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_I64;
		val.vi64 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(U64 v)
	{
		GUARD(MVar::MVar(U64 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_U64;
		val.vu64 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(F32 v)
	{
		GUARD(MVar::MVar(F32 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_F32;
		val.vf32 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(F64 v) 
	{
		GUARD(MVar::MVar(F64 v));

		memset(&val,0,sizeof(val));
		val.vt = VT_F64;
		val.vf64 = v;

		UNGUARD;
	}

	INLINE MVar::MVar(const CHAR* s) 
	{
		GUARD(MVar::MVar(const CHAR* s));

		memset(&val,0,sizeof(val));
		val.vt = VT_STR;
		if( s )
		{
			U32 len = strlen(s);
			if(len)
			{
				val.vstr = MNEW(CHAR[len+1]);CHECK(val.vstr);
				memset(val.vstr,0,(len+1)*sizeof(CHAR));
				strncpy(val.vstr,s,len);
			}
		}

		UNGUARD;
	}

	INLINE MVar::MVar(const WCHAR* s) 
	{
		GUARD(MVar::MVar(const WCHAR* s));

		memset(&val,0,sizeof(val));
		val.vt = VT_WSTR;
		if( s )
		{
			U32 len = wcslen(s);
			if( len )
			{
				val.vwstr = MNEW(WCHAR[len+1]);CHECK(val.vwstr);
				memset(val.vwstr,0,(len+1)*sizeof(WCHAR));
				wcsncpy(val.vwstr,s,len);
			}
		}

		UNGUARD;
	}

	INLINE MVar::MVar(const MObject* o)
	{
		GUARD(MVar::MVar(const MObject* o));

		memset(&val,0,sizeof(val));
		val.vt = VT_OBJ;
		val.vobj = (MObject*)o;
		if( val.vobj ) val.vobj->ref();

		UNGUARD;
	}

	INLINE MVar::~MVar()
	{
		GUARD(MVar::~MVar());

		clear();

		UNGUARD;
	}

	INLINE MVar::operator CHAR() const  
	{
		GUARD(MVar::operator CHAR());

		if( val.vt == VT_I8 ) return val.vi8;

		MVar var;
		var.convert(VT_I8, this);

		return var.val.vi8;

		UNGUARD;
	}

	INLINE MVar::operator U8() const 
	{
		GUARD(MVar::operator U8());

		if (val.vt == VT_U8) return val.vu8;

		MVar var;
		var.convert(VT_U8, this);

		return var.val.vu8;

		UNGUARD;
	}

	INLINE MVar::operator I16() const 
	{
		GUARD(MVar::operator I16());

		if (val.vt == VT_I16) return val.vi16;

		MVar var;
		var.convert(VT_I16, this);

		return var.val.vi16;

		UNGUARD;
	}

	INLINE MVar::operator U16() const  
	{
		GUARD(MVar::operator U16());

		if (val.vt == VT_U16) return val.vu16;

		MVar var;
		var.convert(VT_U16, this);

		return var.val.vu16;

		UNGUARD;
	}

	INLINE MVar::operator I32() const  
	{
		GUARD(MVar::operator I32());

		if (val.vt == VT_I32) return val.vi32;

		MVar var;
		var.convert(VT_I32, this);

		return var.val.vi32;

		UNGUARD;
	}

	INLINE MVar::operator U32() const  
	{
		GUARD(MVar::operator U32());

		if (val.vt == VT_U32) return val.vu32;

		MVar var;
		var.convert(VT_U32, this);

		return var.val.vu32;

		UNGUARD;
	}

	INLINE MVar::operator I64() const  
	{
		GUARD(MVar::operator I64());

		if (val.vt == VT_I64) return val.vi64;

		MVar var;
		var.convert(VT_I64, this);

		return var.val.vi64;

		UNGUARD;
	}

	INLINE MVar::operator U64() const  
	{
		GUARD(MVar::operator U64());

		if (val.vt == VT_U64) return val.vu64;

		MVar var;
		var.convert(VT_U64, this);

		return var.val.vu64;

		UNGUARD;
	}

	INLINE MVar::operator F32() const 
	{
		GUARD(MVar::operator F32());

		if (val.vt == VT_F32) return val.vf32;

		MVar var;
		var.convert(VT_F32, this);

		return var.val.vf32;

		UNGUARD;
	}

	INLINE MVar::operator F64() const 
	{
		GUARD(MVar::operator F64());

		if (val.vt == VT_F64) return val.vf64;

		MVar var;
		var.convert(VT_F64, this);

		return var.val.vf64;

		UNGUARD;
	}

	INLINE MVar::operator CHAR*() const
	{
		GUARD(MVar::operator CHAR*());

		if (val.vt == VT_STR) return val.vstr;
		if (val.vt == VT_NULL) return NULL;
		
		MVar var; var.convert(VT_STR, this);
		if(val.vp){ MDELETE([]((U8*)val.vp)), (VOID*)val.vp = NULL; }
		U32 len = strlen(var.val.vstr);
		(VOID*)val.vp = MNEW(U8[(len+1)*sizeof(CHAR)]);CHECK(val.vp);
		strncpy(((CHAR*)val.vp),var.val.vstr,len);
		return (CHAR*)val.vp;

		UNGUARD;
	}

	INLINE MVar::operator WCHAR*() const
	{
		GUARD(MVar::operator WCHAR*());

		if (val.vt == VT_WSTR) return val.vwstr;
		if (val.vt == VT_NULL) return NULL;
		
		MVar var; var.convert(VT_WSTR, this);
		if(val.vp){ MDELETE([]((U8*)val.vp)), (VOID*)val.vp = NULL;}
		U32 len = wcslen(var.val.vwstr);
		(VOID*)val.vp = MNEW(U8[(len+1)*sizeof(WCHAR)]);CHECK(val.vp);
		wcsncpy(((WCHAR*)val.vp),var.val.vwstr,len);
		return (WCHAR*)val.vp;

		UNGUARD;
	}

	INLINE MVar::operator MObject*() const 
	{
		GUARD(MVar::operator MObject*());

		if (val.vt == VT_OBJ) return val.vobj;
		if (val.vt == VT_NULL) return NULL;

		MVar var;
		var.convert(VT_OBJ, this);		

		return var.val.vobj;

		UNGUARD;
	}	

	INLINE MVar& MVar::operator=(const MVar& var) 
	{
		GUARD(MVar::operator=(const MVar& var));

		clear();
		val.vt = var.val.vt;
		switch (var.val.vt) 
		{
		case VT_VOID:
			break;
		case VT_NULL:
			break;
		case VT_I8:
			val.vi8 = var.val.vi8;
			break;
		case VT_U8:
			val.vu8 = var.val.vu8;
			break;
		case VT_I16:
			val.vi16 = var.val.vi16;
			break;
		case VT_U16:
			val.vu16 = var.val.vu16;
			break;
		case VT_I32:
			val.vi32 = var.val.vi32;
			break;
		case VT_U32:
			val.vu32 = var.val.vu32;
			break;
		case VT_I64:
			val.vi64 = var.val.vi64;
			break;
		case VT_U64:
			val.vu64 = var.val.vu64;
			break;
		case VT_F32:
			val.vf32 = var.val.vf32;
			break;
		case VT_F64:
			val.vf64 = var.val.vf64;
			break;
		case VT_STR:			
			if( var.val.vstr )
			{
				U32 len = strlen(var.val.vstr);
				if(len)
				{
					val.vstr = MNEW(CHAR[len+1]);CHECK(val.vstr);
					memset(val.vstr,0,(len+1)*sizeof(CHAR));
					strncpy(val.vstr,var.val.vstr,len);
				}
			}
			break;
		case VT_WSTR:
			if( var.val.vwstr )
			{
				U32 len = wcslen(var.val.vwstr);
				if( len )
				{
					val.vwstr = MNEW(WCHAR[len+1]);CHECK(val.vwstr);
					memset(val.vwstr,0,(len+1)*sizeof(WCHAR));
					wcsncpy(val.vwstr,var.val.vwstr,len);
				}
			}
			break;
		case VT_OBJ:
			if( var.val.vobj )
			{
				val.vobj = var.val.vobj;
				val.vobj->ref();
			}
			break;
		default:
			MAssert(VA("Error : the type(%d) is unknown!",var.val.vt));
			break;
		}
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(CHAR v) 
	{
		GUARD(MVar::operator=(CHAR v));

		if( val.vt != VT_I8) 
		{			
			clear();
			val.vt = VT_I8;
		}
		val.vi8 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(U8 v) 
	{
		GUARD(MVar::operator=(U8 v));

		if (val.vt != VT_U8) 
		{
			clear();
			val.vt = VT_U8;
		}
		val.vu8 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(I16 v) 
	{
		GUARD(MVar::operator=(I16 v));

		if (val.vt != VT_I16) 
		{
			clear();
			val.vt = VT_I16;
		}
		val.vi16 = v;

		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(U16 v) 
	{
		GUARD(MVar::operator=(U16 v));

		if (val.vt != VT_U16) 
		{
			clear();
			val.vt = VT_U16;
		}
		val.vu16 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(I32 v) 
	{
		GUARD(MVar::operator=(I32 v));

		if (val.vt != VT_I32) 
		{		
			clear();
			val.vt = VT_I32;
		}
		val.vi32 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(U32 v) 
	{
		GUARD(MVar::operator=(U32 v));

		if(val.vt != VT_U32) 
		{
			clear();
			val.vt = VT_U32;
		}
		val.vu32 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(I64 v) 
	{
		GUARD(MVar::operator=(I64 v));

		if (val.vt != VT_I8) 
		{
			clear();
			val.vt = VT_I8;
		}
		val.vi64 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(U64 v) 
	{
		GUARD(MVar::operator=(U64 v));

		if (val.vt != VT_U64) 
		{
			clear();
			val.vt = VT_U64;
		}
		val.vu64 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(F32 v) 
	{
		GUARD(MVar::operator=(F32 v));

		if (val.vt != VT_F32) 
		{			
			clear();
			val.vt = VT_F32;
		}
		val.vf32 = v;
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(F64 v) 
	{
		GUARD(MVar::operator=(F64 v));

		if (val.vt != VT_F64) 
		{
			clear();
			val.vt = VT_F64;
		}
		val.vf64 = v;

		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(const CHAR* s) 
	{
		GUARD(MVar::operator=(const CHAR* s));

		clear();
		val.vt = VT_STR;
		val.vstr = NULL;
		if( s )
		{
			U32 len = strlen(s);
			if(len)
			{
				val.vstr = MNEW(CHAR[len+1]);CHECK(val.vstr);
				memset(val.vstr,0,(len+1)*sizeof(CHAR));
				strncpy(val.vstr,s,len);
			}
		}
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(const WCHAR* s) 
	{
		GUARD(MVar::operator=(const WCHAR* s));

		clear();
		val.vt = VT_WSTR;
		val.vwstr = NULL;
		if( s )
		{
			U32 len = wcslen(s);
			if( len )
			{
				val.vwstr = MNEW(WCHAR[len+1]);CHECK(val.vwstr);
				memset(val.vwstr,0,(len+1)*sizeof(WCHAR));
				wcsncpy(val.vwstr,s,len);
			}
		}
		return *this;

		UNGUARD;
	}

	INLINE MVar& MVar::operator=(const MObject* o) 
	{
		GUARD(MVar::operator=(const MObject* o));

		clear();
		val.vt = VT_OBJ;
		val.vobj = (MObject*)o;
		if( val.vobj != NULL ) val.vobj->ref();
		return *this;

		UNGUARD;
	}	
	
	INLINE BOOL MVar::operator==(const MVar& var) const
	{
		GUARD(MVar::operator==(const MVar& var));

		if( val.vt != var.val.vt ) return FALSE;
		switch( var.val.vt ) 
		{
		case VT_VOID:
		case VT_NULL:
			return TRUE;
		case VT_I8:
			return val.vi8 == var.val.vi8 ? TRUE : FALSE; 
		case VT_U8:
			return val.vu8 == var.val.vu8 ? TRUE : FALSE; 
		case VT_I16:
			return val.vi16 == var.val.vi16 ? TRUE : FALSE; 
		case VT_U16:
			return val.vu16 == var.val.vu16 ? TRUE : FALSE; 
		case VT_I32:
			return val.vi32 == var.val.vi32 ? TRUE : FALSE; 
		case VT_U32:
			return val.vu32 == var.val.vu32 ? TRUE : FALSE; 
		case VT_I64:
			return val.vi64 == var.val.vi64 ? TRUE : FALSE; 
		case VT_U64:
			return val.vu64 == var.val.vu64 ? TRUE : FALSE; 
		case VT_F32:
			return val.vf32 == var.val.vf32 ? TRUE : FALSE; 
		case VT_F64:
			return val.vf64 == var.val.vf64 ? TRUE : FALSE; 
		case VT_STR:
			return ((val.vstr==NULL && var.val.vstr==NULL) || (val.vstr && var.val.vstr && strlen(val.vstr) == strlen(var.val.vstr) && stricmp(val.vstr,var.val.vstr)==0)) ? TRUE : FALSE;
		case VT_WSTR:
			return ((val.vwstr==NULL && var.val.vwstr==NULL) || (val.vwstr && var.val.vwstr && wcslen(val.vwstr)==wcslen(var.val.vwstr) && wcscmp(val.vwstr,var.val.vwstr)==0)) ? TRUE : FALSE;
		case VT_OBJ:
			return val.vobj == var.val.vobj ? TRUE : FALSE; 
		default:
			MAssert(VA("Error : the type(%d) is unknown!",var.val.vt));
			return FALSE;
		}
		return FALSE;

		UNGUARD;
	}

	INLINE BOOL MVar::operator!=(const MVar& var) const
	{
		GUARD(MVar::operator!=(const MVar& var));

		return !(*this == var) ? TRUE : FALSE; 

		UNGUARD;
	}

	INLINE U32 MVar::type() const
	{
		GUARD(MVar::type());

		return val.vt;

		UNGUARD;
	}

	INLINE CHAR MVar::value(CHAR v) const
	{
		GUARD(MVar::value(CHAR v));

		return *this;

		UNGUARD;
	}

	INLINE U8 MVar::value(U8 v) const
	{
		GUARD(MVar::value(U8 v));

		return *this;

		UNGUARD;
	}

	INLINE I16 MVar::value(I16 v) const
	{
		GUARD(MVar::value(I16 v));

		return *this;

		UNGUARD;
	}

	INLINE U16 MVar::value(U16 v) const
	{
		GUARD(MVar::value(U16 v));

		return *this;

		UNGUARD;
	}

	INLINE I32 MVar::value(I32 v) const
	{
		GUARD(MVar::value(I32 v));

		return *this;

		UNGUARD;
	}

	INLINE U32 MVar::value(U32 v) const
	{
		GUARD(MVar::value(U32 v));

		return *this;

		UNGUARD;
	}

	INLINE I64 MVar::value(I64 v) const
	{
		GUARD(MVar::value(I64 v));

		return *this;

		UNGUARD;
	}

	INLINE U64 MVar::value(U64 v) const
	{
		GUARD(MVar::value(U64 v));

		return *this;

		UNGUARD;
	}

	INLINE F32 MVar::value(F32 v) const
	{
		GUARD(MVar::value(F32 v));

		return *this;

		UNGUARD;
	}

	INLINE F64 MVar::value(F64 v) const
	{
		GUARD(MVar::value(F64 v));

		return *this;

		UNGUARD;
	}

	INLINE CHAR* MVar::value(const CHAR* s) const
	{
		GUARD(MVar::value(const CHAR* s));

		return *this;

		UNGUARD;
	}

	INLINE WCHAR* MVar::value(const WCHAR* s) const
	{
		GUARD(MVar::value(const WCHAR* s));

		return *this;

		UNGUARD;
	}

	INLINE MObject* MVar::value(const MObject* o) const
	{
		GUARD(MVar::value(const MObject* o));

		return *this;

		UNGUARD;
	}

	INLINE MVar MVar::value(const MVar& v) const 
	{
		GUARD(MVar::value(const MVar& v));

		return *this;

		UNGUARD;
	}

	INLINE VOID MVar::clear() 
	{
		GUARD(MVar::clear);

		switch (val.vt) 
		{
		case VT_STR:
			if( val.vstr ){ MDELETE([]val.vstr), val.vstr = NULL;}
			break;
		case VT_WSTR:
			if( val.vwstr ){ MDELETE([]val.vwstr), val.vwstr = NULL;}
			break;
		case VT_OBJ:
			if( val.vobj ) { val.vobj->unref(), val.vobj = NULL;};
		default:			
			break;
		}
		if(val.vp) { MDELETE([]((U8*)val.vp)), val.vp = NULL; }
		memset(&val,0,sizeof(val));

		UNGUARD;
	}

	INLINE VOID MVar::convert(VT vt, const MVar* var) 
	{
		GUARD(MVar::convert);

		if( var == NULL ) var = this;
		if((this != var) || (vt != val.vt))
		{
			// clear the old type
			clear();

			// store the new type
			val.vt = vt;
			switch (vt) 
			{
			case VT_NULL:
				break;
			case VT_I8:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vi8 = var->val.vi8;
						break;
					case VT_U8:
						val.vi8 = var->val.vu8;
						break;
					case VT_I16:
						val.vi8 = var->val.vi16;
						break;
					case VT_U16:
						val.vi8 = var->val.vu16;
						break;
					case VT_I32:
						val.vi8 = var->val.vi32;
						break;
					case VT_U32:
						val.vi8 = var->val.vu32;
						break;
					case VT_I64:
						val.vi8 = var->val.vi64;
						break;
					case VT_U64:
						val.vi8 = var->val.vu64;
						break;
					case VT_F32:
						val.vi8 = var->val.vf32;
						break;
					case VT_F64:
						val.vi8 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_I8!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_I8!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_I8!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_U8:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vu8 = var->val.vi8;
						break;
					case VT_U8:
						val.vu8 = var->val.vu8;
						break;
					case VT_I16:
						val.vu8 = var->val.vi16;
						break;
					case VT_U16:
						val.vu8 = var->val.vu16;
						break;
					case VT_I32:
						val.vu8 = var->val.vi32;
						break;
					case VT_U32:
						val.vu8 = var->val.vu32;
						break;
					case VT_I64:
						val.vu8 = var->val.vi64;
						break;
					case VT_U64:
						val.vu8 = var->val.vu64;
						break;
					case VT_F32:
						val.vu8 = var->val.vf32;
						break;
					case VT_F64:
						val.vu8 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_U8!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_U8!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_U8!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_I16:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vi16 = var->val.vi8;
						break;
					case VT_U8:
						val.vi16 = var->val.vu8;
						break;
					case VT_I16:
						val.vi16 = var->val.vi16;
						break;
					case VT_U16:
						val.vi16 = var->val.vu16;
						break;
					case VT_I32:
						val.vi16 = var->val.vi32;
						break;
					case VT_U32:
						val.vi16 = var->val.vu32;
						break;
					case VT_I64:
						val.vi16 = var->val.vi64;
						break;
					case VT_U64:
						val.vi16 = var->val.vu64;
						break;
					case VT_F32:
						val.vi16 = var->val.vf32;
						break;
					case VT_F64:
						val.vi16 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_I16!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_I16!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_I16!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_U16:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vu16 = var->val.vi8;
						break;
					case VT_U8:
						val.vu16 = var->val.vu8;
						break;
					case VT_I16:
						val.vu16 = var->val.vi16;
						break;
					case VT_U16:
						val.vu16 = var->val.vu16;
						break;
					case VT_I32:
						val.vu16 = var->val.vi32;
						break;
					case VT_U32:
						val.vu16 = var->val.vu32;
						break;
					case VT_I64:
						val.vu16 = var->val.vi64;
						break;
					case VT_U64:
						val.vu16 = var->val.vu64;
						break;
					case VT_F32:
						val.vu16 = var->val.vf32;
						break;
					case VT_F64:
						val.vu16 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_U16!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_U16!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_U16!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_I32:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vi32 = var->val.vi8;
						break;
					case VT_U8:
						val.vi32 = var->val.vu8;
						break;
					case VT_I16:
						val.vi32 = var->val.vi16;
						break;
					case VT_U16:
						val.vi32 = var->val.vu16;
						break;
					case VT_I32:
						val.vi32 = var->val.vi32;
						break;
					case VT_U32:
						val.vi32 = var->val.vu32;
						break;
					case VT_I64:
						val.vi32 = var->val.vi64;
						break;
					case VT_U64:
						val.vi32 = var->val.vu64;
						break;
					case VT_F32:
						val.vi32 = var->val.vf32;
						break;
					case VT_F64:
						val.vi32 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_I32!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_I32!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_I32!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_U32:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vu32 = var->val.vi8;
						break;
					case VT_U8:
						val.vu32 = var->val.vu8;
						break;
					case VT_I16:
						val.vu32 = var->val.vi16;
						break;
					case VT_U16:
						val.vu32 = var->val.vu16;
						break;
					case VT_I32:
						val.vu32 = var->val.vi32;
						break;
					case VT_U32:
						val.vu32 = var->val.vu32;
						break;
					case VT_I64:
						val.vu32 = var->val.vi64;
						break;
					case VT_U64:
						val.vu32 = var->val.vu64;
						break;
					case VT_F32:
						val.vu32 = var->val.vf32;
						break;
					case VT_F64:
						val.vu32 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_U32!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_U32!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_U32!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_I64:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vi64 = var->val.vi8;
						break;
					case VT_U8:
						val.vi64 = var->val.vu8;
						break;
					case VT_I16:
						val.vi64 = var->val.vi16;
						break;
					case VT_U16:
						val.vi64 = var->val.vu16;
						break;
					case VT_I32:
						val.vi64 = var->val.vi32;
						break;
					case VT_U32:
						val.vi64 = var->val.vu32;
						break;
					case VT_I64:
						val.vi64 = var->val.vi64;
						break;
					case VT_U64:
						val.vi64 = var->val.vu64;
						break;
					case VT_F32:
						val.vi64 = var->val.vf32;
						break;
					case VT_F64:
						val.vi64 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_I64!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_I64!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_I64!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_U64:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vu64 = var->val.vi8;
						break;
					case VT_U8:
						val.vu64 = var->val.vu8;
						break;
					case VT_I16:
						val.vu64 = var->val.vi16;
						break;
					case VT_U16:
						val.vu64 = var->val.vu16;
						break;
					case VT_I32:
						val.vu64 = var->val.vi32;
						break;
					case VT_U32:
						val.vu64 = var->val.vu32;
						break;
					case VT_I64:
						val.vu64 = var->val.vi64;
						break;
					case VT_U64:
						val.vu64 = var->val.vu64;
						break;
					case VT_F32:
						val.vu64 = var->val.vf32;
						break;
					case VT_F64:
						val.vu64 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_U64!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_U64!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_U64!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_F32:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vf32 = var->val.vi8;
						break;
					case VT_U8:
						val.vf32 = var->val.vu8;
						break;
					case VT_I16:
						val.vf32 = var->val.vi16;
						break;
					case VT_U16:
						val.vf32 = var->val.vu16;
						break;
					case VT_I32:
						val.vf32 = var->val.vi32;
						break;
					case VT_U32:
						val.vf32 = var->val.vu32;
						break;
					case VT_I64:
						val.vf32 = var->val.vi64;
						break;
					case VT_U64:
						val.vf32 = (I64)var->val.vu64;
						break;
					case VT_F32:
						val.vf32 = var->val.vf32;
						break;
					case VT_F64:
						val.vf32 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_F32!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_F32!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_F32!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_F64:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vf64 = var->val.vi8;
						break;
					case VT_U8:
						val.vf64 = var->val.vu8;
						break;
					case VT_I16:
						val.vf64 = var->val.vi16;
						break;
					case VT_U16:
						val.vf64 = var->val.vu16;
						break;
					case VT_I32:
						val.vf64 = var->val.vi32;
						break;
					case VT_U32:
						val.vf64 = var->val.vu32;
						break;
					case VT_I64:
						val.vf64 = var->val.vi64;
						break;
					case VT_U64:
						val.vf64 = (I64)var->val.vu64;
						break;
					case VT_F32:
						val.vf64 = var->val.vf32;
						break;
					case VT_F64:
						val.vf64 = var->val.vf64;
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_F64!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_F64!");
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_F64!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_STR:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vstr = MNEW(CHAR[8+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%d",var->val.vi8);
						break;
					case VT_U8:
						val.vstr = MNEW(CHAR[8+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%u",var->val.vu8);
						break;
					case VT_I16:
						val.vstr = MNEW(CHAR[16+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%d",var->val.vi16);
						break;
					case VT_U16:
						val.vstr = MNEW(CHAR[16+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%u",var->val.vu16);
						break;
					case VT_I32:
						val.vstr = MNEW(CHAR[32+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%d",var->val.vi32);
						break;
					case VT_U32:
						val.vstr = MNEW(CHAR[32+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%u",var->val.vu32);
						break;
					case VT_I64:
						val.vstr = MNEW(CHAR[64+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%d",var->val.vi64);
						break;
					case VT_U64:
						val.vstr = MNEW(CHAR[64+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%u",var->val.vu64);
						break;
					case VT_F32:
						val.vstr = MNEW(CHAR[32+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%f",var->val.vf32);
						break;
					case VT_F64:
						val.vstr = MNEW(CHAR[64+1]);CHECK(val.vstr);
						sprintf(val.vstr,"%f",var->val.vf64);
						break;
					case VT_STR:
						if( var->val.vstr )
						{
							U32 len = strlen(var->val.vstr);
							if(len)
							{
								val.vstr = MNEW(CHAR[len+1]);CHECK(val.vstr);
								memset(val.vstr,0,(len+1)*sizeof(CHAR));
								strncpy(val.vstr,var->val.vstr,len);
							}
						}
						break;
					case VT_WSTR:
						if( var->val.vwstr )
						{
							U32 len = wcslen(var->val.vwstr);
							if( len )
							{
								val.vstr = MNEW(CHAR[len+1]);CHECK(val.vstr);
								memset(val.vstr,0,(len+1)*sizeof(CHAR));
								wcstombs(val.vstr,var->val.vwstr,len);
							}
						}						
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_STR!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_WSTR:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						val.vwstr = MNEW(WCHAR[8+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%d",var->val.vi8);
						break;
					case VT_U8:
						val.vwstr = MNEW(WCHAR[8+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%u",var->val.vu8);
						break;
					case VT_I16:
						val.vwstr = MNEW(WCHAR[16+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%d",var->val.vi16);
						break;
					case VT_U16:
						val.vwstr = MNEW(WCHAR[16+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%u",var->val.vu16);
						break;
					case VT_I32:
						val.vwstr = MNEW(WCHAR[32+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%d",var->val.vi32);
						break;
					case VT_U32:
						val.vwstr = MNEW(WCHAR[32+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%u",var->val.vu32);
						break;
					case VT_I64:
						val.vwstr = MNEW(WCHAR[64+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%d",var->val.vi64);
						break;
					case VT_U64:
						val.vwstr = MNEW(WCHAR[64+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%u",var->val.vu64);
						break;
					case VT_F32:
						val.vwstr = MNEW(WCHAR[32+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%f",var->val.vf32);
						break;
					case VT_F64:
						val.vwstr = MNEW(WCHAR[64+1]);CHECK(val.vwstr);
						swprintf(val.vwstr,L"%f",var->val.vf64);
						break;
					case VT_STR:
						if( var->val.vstr )
						{
							U32 len = strlen(var->val.vstr);
							if(len)
							{
								val.vwstr = MNEW(WCHAR[len+1]);CHECK(val.vwstr);
								memset(val.vwstr,0,(len+1)*sizeof(WCHAR));
								mbstowcs(val.vwstr,var->val.vstr,len);
							}
						}
						break;
					case VT_WSTR:
						if( var->val.vwstr )
						{
							U32 len = wcslen(var->val.vwstr);
							if( len )
							{
								val.vwstr = MNEW(WCHAR[len+1]);CHECK(val.vwstr);
								memset(val.vwstr,0,(len+1)*sizeof(CHAR));
								wcsncpy(val.vwstr,var->val.vwstr,len);
							}
						}						
						break;
					case VT_OBJ:
						MAssert("Error : can`t convert the type from VT_OBJ to VT_WSTR!");
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			case VT_OBJ:
				{
					switch (var->val.vt) 
					{
					case VT_NULL:
						break;
					case VT_I8:
						MAssert("Error : can`t convert the type from VT_I8 to VT_OBJ!");
						break;
					case VT_U8:
						MAssert("Error : can`t convert the type from VT_U8 to VT_OBJ!");
						break;
					case VT_I16:
						MAssert("Error : can`t convert the type from VT_I16 to VT_OBJ!");
						break;
					case VT_U16:
						MAssert("Error : can`t convert the type from VT_U16 to VT_OBJ!");
						break;
					case VT_I32:
						MAssert("Error : can`t convert the type from VT_I32 to VT_OBJ!");
						break;
					case VT_U32:
						MAssert("Error : can`t convert the type from VT_U32 to VT_OBJ!");
						break;
					case VT_I64:
						MAssert("Error : can`t convert the type from VT_I64 to VT_OBJ!");
						break;
					case VT_U64:
						MAssert("Error : can`t convert the type from VT_U64 to VT_OBJ!");
						break;
					case VT_F32:
						MAssert("Error : can`t convert the type from VT_F32 to VT_OBJ!");
						break;
					case VT_F64:
						MAssert("Error : can`t convert the type from VT_F64 to VT_OBJ!");
						break;
					case VT_STR:
						MAssert("Error : can`t convert the type from VT_STR to VT_OBJ!");
						break;
					case VT_WSTR:
						MAssert("Error : can`t convert the type from VT_WSTR to VT_OBJ!");
						break;
					case VT_OBJ:
						if( var->val.vobj ) { val.vobj = var->val.vobj; val.vobj->ref(); }
						break;
					default:
						MAssert(VA("Error : the type(%d) is unknown!",vt));
						break;
					}
				}
				break;
			default:
				MAssert(VA("Error : the type(%d) is unknown!",vt));
				break;
			}
		}

		UNGUARD;
	}

//////////////////////////////////////////////////////////////////////////

NAMESPACE_END

//////////////////////////////////////////////////////////////////////////

